package com.luoqiu.cinema.service;

import com.luoqiu.cinema.entity.UserInfo;
import com.luoqiu.cinema.mapper.UserInfoMapper;
import com.luoqiu.cinema.util.UserInfoUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserInfoUtil userInfoUtil;


    @Transactional(rollbackFor = Exception.class)
    public void regis(UserInfo userInfo) {
        if (userInfo.getId() == null) {
            userInfo.setAdmin(0);
            userInfoMapper.insertSelective(userInfo);
        } else {
            userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }
    }


    public UserInfo login(String username) {
        UserInfo userInfo = userInfoMapper.loadByUsername(username);
        if (userInfo == null) {
            return null;
        }
        List<String> role = new ArrayList<>();
        if (null == userInfo.getAdmin() || userInfo.getAdmin() == 0) {
            role.add("ordinary");
        } else {
            role.add("admin");
        }
        userInfo.setRoles(role);
        return userInfo;
    }

    public void sign() {
        userInfoUtil.score(5);
    }

    public void luck() {
        userInfoUtil.score(-50);
    }
}
